package com.kcs.fxmargin.action;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.kcs.common.utils.Constant;
import com.kcs.fxmargin.action.form.SetupUserProfileActionForm;
import com.kcs.fxmargin.bo.UserProFileBO;
import com.kcs.fxmargin.service.RoleService;
import com.kcs.fxmargin.service.UsersService;

public class SetupUserProfileAction extends FXAction {
	private Logger log = Logger.getLogger(Constant.LOG_FX);
	
	RoleService roleService		= new RoleService();
	UsersService userService 	= new UsersService();
	
	@Override
	public ActionForward process(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws Exception{ 
		try{

			log.debug("SetupUserProfileAction : Start");			
			
			SetupUserProfileActionForm formBean = (SetupUserProfileActionForm) form;
			
			if("init".equals(formBean.getAction())){
				formBean.resetExpect();
				formBean.setUserProFileList(new ArrayList<UserProFileBO>());
				formBean.setRoleList(roleService.getRole());
			} else if("search".equals(formBean.getAction())){
				search(formBean, request);
			} else if("delete".equals(formBean.getAction())){
				delete(formBean, request);
			}
			
			formBean.setAction("init");
			log.debug("SetupUserProfileAction : End");
			
		}catch(Exception e){
			log.error(e.getMessage());
			return (mapping.findForward("error"));
		}
		
		
		return mapping.findForward("setupUserProfile");
	}
	
	public void search(SetupUserProfileActionForm formBean, HttpServletRequest request) throws Exception{
		log.debug("SetupUserProfileAction : search : Start");
		
		UserProFileBO bo = new UserProFileBO();
		bo.setUserId(formBean.getUserCode());
		bo.setUserName(formBean.getUserName());
		bo.setRoleId(formBean.getRole());
		
		formBean.setUserProFileList(userService.searchUser(bo));
		request.setAttribute(Constant.SHOW_RESULT, Constant.SHOW_RESULT);
		
		log.debug("SetupUserProfileAction : search : End");
	}
	
	public void delete(SetupUserProfileActionForm formBean, HttpServletRequest request) throws Exception{
		log.debug("SetupUserProfileAction : delete : Start");
		
		if(formBean.getDeleteId() != null && !"".equals(formBean.getDeleteId())){
			userService.deleteUser(formBean.getDeleteId());
			
			UserProFileBO userBo = null;
			
			for(UserProFileBO bo : formBean.getUserProFileList()){
				if(formBean.getDeleteId().equals(bo.getId())){
					userBo = bo;
				}
			}
			
			formBean.getUserProFileList().remove(userBo);
			
			int index = 1;
			
			for(UserProFileBO bo : formBean.getUserProFileList()){
				bo.setRowNum(index);
				index++; 
			}
		}
		request.setAttribute(Constant.SHOW_RESULT, Constant.SHOW_RESULT);
		
		log.debug("SetupUserProfileAction : delete : End");
	}
	
}
